home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 341 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.0 KB

  1. Path: fido.asd.sgi.com!austern
  2. From: clamage@Eng.Sun.COM (Steve Clamage)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: deriving from a typedef name
  5. Date: 08 Feb 1996 14:31:32 PST
  6. Organization: Sun Microsystems Inc.
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4fdp1l$6ce@engnews1.Eng.Sun.COM>
  9. References: <4fdinq$8ds@cnn.Princeton.EDU>
  10. Reply-To: clamage@Eng.Sun.COM
  11. NNTP-Posting-Host: isolde.mti.sgi.com
  12. X-Original-Date: 8 Feb 1996 21:13:25 GMT
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMRp5z0y4NqrwXLNJAQHOegH7BExx3+fGnKReOvv6YVOLW1DvUzG+1Ab+
  15.     rVqszWUMSzoQx5cRf2OJn+VSk9mxD++/eZjD74XMnUnDwFyXCP233Q==
  16.     =tZlD
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. In article 8ds@cnn.Princeton.EDU, tim@franck.Princeton.EDU (Tim Hollebeek) writes:
  20. >I suspect this is correct behavior, but it sure is confusing, so I
  21. >though I'd double check:
  22. >
  23. >-----
  24. >template <class T>
  25. >class nVector {
  26. >};
  27. >
  28. >typedef nVector<double> Vector; // workaround for compiler that
  29. >                                // doesn't handle <class T = double> well
  30. >
  31. >class Point : public Vector { // derive from typedef name
  32. >public:
  33. >    Point(const Vector& v) { Vector::Vector(v); } // ***
  34. >};
  35. >-----
  36. >
  37. >At ***, I get: test.C:9: no method `nVector<double>::Vector'
  38. >for obvious reasons.  However, it's not so obvious if you are just
  39. >looking at the declaration of class Point, and the alternatives:
  40. >
  41. >Vector::nVector<double>(v); // parse error
  42. >nVector<double>::nVector<double>(v); // parse error
  43. >
  44. >don't work; I ended up having to change point to derive from
  45. >nVector<double>.  Are all three of the above really disallowed by
  46. >the standard?  If yes, is there a way to call the copy constructor
  47. >of the inherited class?
  48.  
  49. I'm having a little trouble understanding what your question is, but I
  50. see in particular two different issues.
  51.  
  52. 1. A typedef name is a synonym for a type, and may be used wherever a
  53. type name is needed. The name of a constructor is not a type name, but
  54. must be spelled the same as its actual class name. So Vector::Vector is
  55. not the name of a constructor for nVector<double>. A construtor name is
  56. nVector<double>::nVector<double>. I believe that can also be written as 
  57. Vector::nVector<double>, but I don't think you would want to.
  58.  
  59. 2. You cannot call a constructor directly like a normal function.
  60. So as not to get lost in template syntax, given a type T, you can't
  61. call a constructor directly as T::T() or T::T(t). You can cause a
  62. constructor to be invoked to create an anonymous object, as in
  63.     foo(T());
  64. where function foo takes a T parameter. Similarly, in the Point copy
  65. constructor you could invoke a Vector constructor by its proper name,
  66. but I doubt that is the effect you want. The expression nVector<double>(v)
  67. creates an anonymous copy of v and then destroys it at the end of the block.
  68.  
  69. ---
  70. Steve Clamage, stephen.clamage@eng.sun.com
  71. ---
  72. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  73.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  74.   in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
  75.